/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import BDD.gestionnaires.GestionnaireAlbum;
import BDD.gestionnaires.GestionnaireCommentaire;
import BDD.gestionnaires.GestionnaireNotification;
import BDD.gestionnaires.GestionnairePhoto;
import BDD.gestionnaires.GestionnaireUtilisateur;
import BDD.modeles.Album;
import BDD.modeles.Commentaire;
import BDD.modeles.Notification;
import BDD.modeles.Photo;
import BDD.modeles.Utilisateur;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tenrousuke
 */
@WebServlet(name = "ServletCommentaire", urlPatterns = {"/Commentaire"})
public class ServletCommentaire extends HttpServlet {
    
    @EJB
    private GestionnairePhoto gestionnairePhoto;
    
    @EJB
    private GestionnaireCommentaire gestionnaireCommentaire;
    
    @EJB
    private GestionnaireUtilisateur gestionnaireUtilisateur;
    
    @EJB
    private GestionnaireNotification gestionnaireNotification;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String photoId = request.getParameter("photo");
        String post = request.getParameter("post");
        String forwardTo = "";
        String message = "";

        if(photoId != null){
            
            if(post != null){
                
                HttpSession session = request.getSession();
                String texte = request.getParameter("texte");

                Photo photo = gestionnairePhoto.getPhotosById(Integer.valueOf(photoId));
                Utilisateur user = gestionnaireUtilisateur.getUser( (String)(session.getAttribute("loginUser")), (String)(session.getAttribute("mdpUser")) );
                if(user == null)
                    return;
                
                Commentaire com = gestionnaireCommentaire.creeCommentaire(user, photo, texte);
                Utilisateur owner = gestionnairePhoto.getOwnerOf(photo);
                
                Notification n = gestionnaireNotification.creeNotification(owner, com);
                
                response.setContentType("text/html;charset=UTF-8");
                try (PrintWriter out = response.getWriter()) {
                    out.println("Ok");
                }
                
                
            }else{
                HttpSession session = request.getSession();

                Collection<Commentaire> comPho = gestionnairePhoto.getCommentaireOfPhotos(Integer.valueOf(photoId));

                request.setAttribute("commentaireList", comPho );

                forwardTo = "commentaireList.jsp";
                message = "";
            }
            
        }
        
        if(post == null){
            RequestDispatcher dp = request.getRequestDispatcher(forwardTo + message);  
            dp.forward(request, response); 
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
